package com.moorror.codegenvel.db.sqlite.dao.impl;

import com.moorror.codegenvel.db.sqlite.dao.BaseDAO;
import com.moorror.codegenvel.db.sqlite.dao.CodeGenXRenderTableDao;
import com.moorror.codegenvel.db.sqlite.entity.CodeGenXRenderTable;
import com.moorror.codegenvel.utils.DateUtils;
import com.moorror.codegenvel.utils.SqliteUtils;
import org.apache.commons.collections4.CollectionUtils;

import java.sql.Connection;
import java.util.List;

/**
 * @Author jiyanghuang
 * @Date 2025/8/21 07:57
 */
public class CodeGenXRenderTableDaoImpl extends BaseDAO<CodeGenXRenderTable> implements CodeGenXRenderTableDao<CodeGenXRenderTable> {

    private static CodeGenXRenderTableDaoImpl instance;

    private CodeGenXRenderTableDaoImpl() {

    }

    public static synchronized CodeGenXRenderTableDaoImpl getInstance() {
        if (null == instance) {
            instance = new CodeGenXRenderTableDaoImpl();
        }
        return instance;
    }

    /**
     * 名称不能包含$符
     * 以$作为分割
     */
    private static final String SELECT_SQL = " select id,kf,name_table, name_table_java, ext_column_json, create_time,update_time from codegen_xrender_table ";

    @Override
    public String tableName() {
        return "codegen_xrender_table";
    }

    @Override
    public void insert(Connection conn, CodeGenXRenderTable[] ary) {
        if (ary == null || ary.length == 0) {
            return;
        }
        //数据库中存在自增列后，SQLite 就会创建一个 sqlite_sequence 表。所有表的自增列都共享这个表，sqlite_sequence 分别维护着每个自增列的当前值，所以自增列的计数也是单独的。
        String sql = "insert into codegen_xrender_table(kf,name_table, name_table_java, ext_column_json,create_time,update_time) values(?,?,?,?,?,?)";
        int size = ary.length;
        if (size == 1) {
            CodeGenXRenderTable po = ary[0];
            update(conn, sql, po.getKf(), po.getNameTable(), po.getNameTableJava(), po.getExtColumnJson(), po.getCreateTime(), po.getUpdateTime());
        } else {
            Object[][] args = new Object[size][5];
            for (int i = 0; i < size; i++) {
                CodeGenXRenderTable po = ary[i];
                args[i][0] = po.getKf();
                args[i][1] = po.getNameTable();
                args[i][2] = po.getNameTableJava();
                args[i][3] = po.getExtColumnJson();
                args[i][4] = po.getCreateTime();
                args[i][5] = po.getUpdateTime();
            }
            updateBatch(conn, sql, args);
        }
    }

    @Override
    public CodeGenXRenderTable insert(Connection conn, CodeGenXRenderTable po) {
        String sql = "insert into codegen_xrender_table(kf,name_table, name_table_java, ext_column_json, create_time,update_time) values(?,?,?,?,?,?)";
        update(conn, sql, po.getKf(), po.getNameTable(), po.getNameTableJava(), po.getExtColumnJson(), po.getCreateTime(), po.getUpdateTime());
        String sqlSelect = SELECT_SQL + " where id = (select LAST_INSERT_ROWID()) limit 1";
        return getBean(conn, sqlSelect);
    }

    @Override
    public void delete(Connection conn, Integer id) {

    }

    @Override
    public boolean deleteByUnique(Connection conn, CodeGenXRenderTable updateT) {
        String sql = "delete from codegen_xrender_table where kf = ? AND name_table = ? AND name_table_java = ? ";
        return update(conn, sql, updateT.getKf(), updateT.getNameTable(), updateT.getNameTableJava()) > 0;
    }

    @Override
    public void update(Connection conn, CodeGenXRenderTable[] ary) {

    }

    @Override
    public void update(Connection conn, CodeGenXRenderTable codeGenSceneTemplate) {

    }

    @Override
    public boolean updateByUnique(Connection conn, CodeGenXRenderTable updateT) {
        String sql = "update codegen_scenetemplate SET update_time = ? where kf = ? AND name_table = ? AND name_table = ? AND ext_column_json = ? ";
        return update(conn, sql, DateUtils.currentTime(), updateT.getKf(), updateT.getNameTable(), updateT.getNameTableJava(), updateT.getExtColumnJson()) > 0;
    }

    @Override
    public CodeGenXRenderTable findById(Connection conn, Integer id) {
        return null;
    }

    @Override
    public CodeGenXRenderTable findByUnique(Connection conn, CodeGenXRenderTable queryT) {
        String sql = SELECT_SQL + " where kf = ? AND name_table = ?  AND name_table_java = ?   limit 1";
        List<CodeGenXRenderTable> re = queryList(conn, sql, queryT.getKf(), queryT.getNameTable(), queryT.getNameTableJava());
        if (CollectionUtils.isEmpty(re)) {
            return null;
        }
        return re.get(0);
    }

    @Override
    public List<CodeGenXRenderTable> queryRelate(Connection conn, List<String> keys) {
        String inSql = SqliteUtils.inSql(keys);
        String sql = SELECT_SQL + " where kf IN " + inSql;
        return queryList(conn, sql, keys.toArray(new Object[0]));
    }

    @Override
    public List<CodeGenXRenderTable> queryIn(Connection conn, List<String> keys) {
        String inSql = SqliteUtils.inSql(keys);
        String sql = SELECT_SQL + " where name_table IN " + inSql;
        return queryList(conn, sql, keys.toArray(new Object[0]));
    }

    @Override
    public void exchangeShowOrder(Connection conn, Integer showOrder1, Integer showOrder2) {

    }
}
